
package trader;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class BrokerDelegate {

    private BrokerModel model;
    
    private static BrokerDelegate instance = new BrokerDelegate();

    public static BrokerDelegate getInstance() {
        return instance;
    }

    private BrokerDelegate() {
        try {
            Context ctx = new InitialContext();
            model = (BrokerModel)ctx.lookup("java:comp/env/BrokerLookup");
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }

    public Customer addCustomer(String customerId, String name, String address) throws BrokerException {
        Customer cust = new Customer(customerId, name, address);
        model.addCustomer(cust);
        return cust;        
    }

    public Customer getCustomer(String customerId) throws BrokerException {
        return model.getCustomer(customerId);        
    }

    public Customer updateCustomer(String customerId, String name, String address) throws BrokerException {
        Customer cust = new Customer(customerId, name, address);
        model.updateCustomer(cust);
        return cust;
    }
    
    public void deleteCustomer(String customerId) throws BrokerException {     
      model.deleteCustomer(new Customer(customerId));
    }

    public Customer[] getAllCustomers() throws BrokerException {
        return model.getAllCustomers();        
    }
   
    public Stock getStock(String symbol) throws BrokerException {
        return model.getStock(symbol);
    }
    
    public Stock[] getAllStocks() throws BrokerException {
        return model.getAllStocks();
    }
        
    public CustomerShare addCustomerShare(String customerId, String stockSymbol, int quantity) throws BrokerException {
        CustomerShare share = new CustomerShare(customerId, stockSymbol, quantity);
        model.addCustomerShare(share);
        return share;
    }

    public CustomerShare updateCustomerShare(String customerId, String stockSymbol, int quantity) throws BrokerException {
        CustomerShare share = new CustomerShare(customerId, stockSymbol, quantity);
        model.updateCustomerShare(share);
        return share;
    }
             
    public CustomerShare[] getAllCustomerShares(String customerId) throws BrokerException {
        return model.getAllCustomerShares(customerId);
    }
}
